package org.example.day;

import java.util.Arrays;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/10 8:44
 */
public class Solution881 {

    public static void main(String[] args) {
        Solution881 solution = new Solution881();
        System.out.println(solution.numRescueBoats(new int[]{3,2,2,1}, 3));
    }

    public int numRescueBoats(int[] people, int limit) {
        //排序， 最大 跟 自小的组合， 如果大于limit就自己坐船过去
        //因为只能左两个人， 不需要追求每个船必须坐满

        Arrays.sort(people);

        int start=0,sum = 0;
        if(limit <= 0 ){
            return 0;
        }

        for (int i = people.length-1; i >= start; i--) {
            if(people[i] + people[start] <= limit){
                start++;
            }
            sum++;
        }

        return sum;
    }

}
